{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "42813e4e",
   "metadata": {},
   "source": [
    "## 列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9984708",
   "metadata": {},
   "source": [
    "### 列表和数组的区别是什么？\n",
    "    准确的来说，在python中是没有数组类型的，python中取而代之的是列表和元组。 \n",
    "    列表比元组好用，因为元组一旦定义就没法修改。而列表不仅可以和数组一样按索引访问，还有一堆的功能。\n",
    "    列表元素之间没有任何关系，可以是任何类型。数组是只能存储同一种数据类型的结构，但是列表可不受局限。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "071c89fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c']\n"
     ]
    }
   ],
   "source": [
    "# 创建列表\n",
    "\n",
    "list = ['a','b','c']\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e671e3d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "# 访问列表中的元素\n",
    "list = ['a','b','c']\n",
    "print(list[0])\n",
    "print(list[0].title()) # 大写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b152b45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abstract\n"
     ]
    }
   ],
   "source": [
    "# 使用列表中的元素\n",
    "list = ['a','b','c']\n",
    "var = f\"{list[0]}bstract\"\n",
    "print(var)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e976d7c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['edit', 'b', 'c']\n",
      "['1', 'edit', 'b', 'c', 'add']\n",
      "['edit', 'b', 'c', 'add']\n",
      "add\n",
      "['edit', 'b', 'c']\n",
      "edit\n",
      "['b', 'c']\n",
      "['b']\n"
     ]
    }
   ],
   "source": [
    "# 修改、添加、删除元素\n",
    "\n",
    "list = ['a','b','c']\n",
    "\n",
    "# 修改\n",
    "list[0] = 'edit'\n",
    "print(list)\n",
    "\n",
    "\n",
    "# 添加\n",
    "# append 在内容最后追加\n",
    "list.append('add')\n",
    "# insert 在指定位置添加\n",
    "list.insert(0,\"1\")\n",
    "print(list)\n",
    "\n",
    "# 删除\n",
    "# del 删除指定索引的数据\n",
    "del list[0]\n",
    "print(list)\n",
    "# pop 删除元素 使用pop在不指定索引时 默认删除最后一位  指定索引时 则按照索引进行删除\n",
    "pop_list = list.pop()\n",
    "print(pop_list)\n",
    "print(list)\n",
    "pop_list = list.pop(0)\n",
    "print(pop_list)\n",
    "print(list)\n",
    "# remove 根据值来删除元素\n",
    "list.remove('c')\n",
    "print(list)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d5904e4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1', '2', '3', 'a', 'b', 'c']\n",
      "['c', 'b', 'a', '3', '2', '1']\n",
      "['1', '2', '3', 'a', 'b', 'c']\n",
      "['c', 'b', 'a', '3', '2', '1']\n"
     ]
    }
   ],
   "source": [
    "# 列表的排序和打印\n",
    "list = ['a','b','c','1','2','3']\n",
    "\n",
    "#sort 对列表进行永久排序 默认为正序 还可以使用 reverse=True来进行反序排列\n",
    "list.sort()\n",
    "print(list)\n",
    "\n",
    "list.sort(reverse=True)\n",
    "print(list)\n",
    "\n",
    "\n",
    "# sorted 对列表进行临时排序\n",
    "print(sorted(list))\n",
    "print(sorted(list,reverse=True))\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c0929e8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# 获取列表的长度\n",
    "list = ['a','b','c']\n",
    "print(len(list))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be99157b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
